package zl.com.gz_2017029e;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import zl.com.gz_2017029e.adapter.Account;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private DrawerLayout drawerLayout;
    private Toolbar toolbar;
    private Button accountManagBtn,trafficLightBtn;
    private List<Account> accountList = new ArrayList<>();
    private TextView batchTopUp;
    private TextView topUpRecords;
    private TextView manmagerName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        toolbar = (Toolbar)findViewById(R.id.main_toolbar);
        setSupportActionBar(toolbar);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_navhome);
        }
        accountManagBtn = (Button)findViewById(R.id.account_manager_btn);
        accountManagBtn.setOnClickListener(this);
        trafficLightBtn = (Button)findViewById(R.id.TrafficLight_manager_btn);
        trafficLightBtn.setOnClickListener(this);
        replaceFragment(new AccountFrame());
        batchTopUp = (TextView)findViewById(R.id.main_batchtopup_tv);
        batchTopUp.setOnClickListener(this);
        topUpRecords = (TextView)findViewById(R.id.main_topuprecords_tv);
        manmagerName = (TextView)findViewById(R.id.main_managername_tv);
//        initAccounts();
//        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.account_recyclerview);
//        LinearLayoutManager manager = new LinearLayoutManager(this);
//        recyclerView.setLayoutManager(manager);
//        AccountAdapter adapter = new AccountAdapter(accountList);
//        recyclerView.setAdapter(adapter);
    }

//    private void initAccounts(){
//        Account account1 = new Account(R.drawable.car_logo,"辽A10001","张三",73);
//        accountList.add(account1);
//        Account account2 = new Account(R.drawable.car_logo,"辽A10002","李四",23);
//        accountList.add(account2);
//        Account account3 = new Account(R.drawable.car_logo,"辽A10003","王五",27);
//        accountList.add(account3);
//        Account account4 = new Account(R.drawable.car_logo,"辽A10004","赵六",2);
//        accountList.add(account4);
//    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                drawerLayout.openDrawer(GravityCompat.START);
                break;
                default:break;
        }
        return true;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.account_manager_btn:
                replaceFragment(new AccountFrame());
                drawerLayout.closeDrawers();
                batchTopUp.setVisibility(View.VISIBLE);
                topUpRecords.setVisibility(View.VISIBLE);
                manmagerName.setText("账户管理");
                break;
            case R.id.TrafficLight_manager_btn:
//                Intent intent = new Intent(MainActivity.this,TrafficLightFrame.class);
//                startActivity(intent);
                replaceFragment(new TrafficLightFrame());
                drawerLayout.closeDrawers();
                batchTopUp.setVisibility(View.GONE);
                topUpRecords.setVisibility(View.GONE);
                manmagerName.setText("红绿灯管理");
                break;
            case R.id.main_batchtopup_tv:

                break;
                default:break;
        }
    }

    private void replaceFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_framelayout,fragment);
        transaction.commit();
    }

}
